package com.ksd.web.controller.system;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ksd.common.annotation.Log;
import com.ksd.common.core.controller.BaseController;
import com.ksd.common.core.domain.AjaxResult;
import com.ksd.common.core.page.TableDataInfo;
import com.ksd.common.enums.BusinessType;
import com.ksd.common.enums.UserStatus;
import com.ksd.common.utils.StringUtils;
import com.ksd.common.utils.poi.ExcelUtil;
import com.ksd.system.domain.*;
import com.ksd.system.service.*;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 内容分类的类型Controller
 * 
 * @author y.wu
 * @date 2025-02-26
 */
@RestController
@RequestMapping("/system/tvData")
public class SysTvDataController extends BaseController
{
    @Autowired
    private ISysTvDataService sysTvDataService;
    @Autowired
    private ISysTvTypeService sysTvTypeService;
    @Autowired
    private ISysOperLogService sysOperLogService;
    @Autowired
    private ISysMovieService sysMovieService;
    @Autowired
    private ISysMusicDataService sysMusicDataService;


    /**
     * 查询内容分类的类型列表
     */
    @PreAuthorize("@ss.hasPermi('system:tvData:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysTvData sysTvData)
    {
        startPage();
        List<SysTvData> list = sysTvDataService.selectSysTvDataList(sysTvData);
        for (SysTvData tvData : list) {
            SysTvType sysTvType = sysTvTypeService.selectSysTvTypeById(tvData.getTypeId());
            tvData.setContentName(sysTvType.getName());
        }
        return getDataTable(list);
    }

    @GetMapping(value = "/data/{typeId}")
    public AjaxResult getTypeDataList(@PathVariable Long typeId){
        return success(sysTvTypeService.selectSysTvTypeById(Long.valueOf(typeId)));
    }
    /**
     * 导出内容分类的类型列表
     */
    @PreAuthorize("@ss.hasPermi('system:tvData:export')")
    @Log(title = "内容分类的类型", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysTvData sysTvData)
    {
        List<SysTvData> list = sysTvDataService.selectSysTvDataList(sysTvData);
        ExcelUtil<SysTvData> util = new ExcelUtil<SysTvData>(SysTvData.class);
        util.exportExcel(response, list, "内容分类的类型数据");
    }

    /**
     * 获取内容分类的类型详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:tvData:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(sysTvDataService.selectSysTvDataById(id));
    }

    /**
     * 新增内容分类的类型
     */
    @PreAuthorize("@ss.hasPermi('system:tvData:add')")
    @Log(title = "内容分类的类型", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysTvData sysTvData)
    {
        if (!sysTvDataService.checkNameAndTypeIdUnique(sysTvData))
        {
            sysOperLogService.insertOperlogError("新增内容分类类型详情:'" + sysTvData.getName() + "'失败，该类型详情名称已存在");
            return error("新增内容分类类型详情:'" + sysTvData.getName() + "'失败，该类型详情名称已存在");
        }
        sysOperLogService.insertOperlogSuccess("新增内容分类类型详情:'" + sysTvData.getName() + "'成功");
        return toAjax(sysTvDataService.insertSysTvData(sysTvData));
    }

    /**
     * 修改内容分类的类型
     */
    @PreAuthorize("@ss.hasPermi('system:tvData:edit')")
    @Log(title = "内容分类的类型", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysTvData sysTvData)
    {
        if (!sysTvDataService.checkNameAndTypeIdUnique(sysTvData))
        {
            sysOperLogService.insertOperlogError("修改内容分类类型详情:'" + sysTvData.getName() + "'失败，该类型详情名称已存在");
            return error("修改内容分类类型详情:'" + sysTvData.getName() + "'失败，该类型详情名称已存在");
        }
        sysOperLogService.insertOperlogSuccess("修改内容分类类型详情:'" + sysTvData.getName() + "'成功");
        return toAjax(sysTvDataService.updateSysTvData(sysTvData));
    }

    /**
     * 删除内容分类的类型
     */
    @PreAuthorize("@ss.hasPermi('system:tvData:remove')")
    @Log(title = "内容分类的类型", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        for (Long dataId : ids) {
            
            SysTvData sysTvData = sysTvDataService.selectSysTvDataById(dataId);
            Long typeId = sysTvData.getTypeId();
            if (SysTvType.movieDefaultId.equals(String.valueOf(typeId))){
                SysMovie sysMovie = new SysMovie();
                sysMovie.setTypeData(dataId);
                List<SysMovie> movieList = sysMovieService.selectSysMovieList(sysMovie);
                if (movieList != null && movieList.size() > 0) {
                    sysOperLogService.insertOperlogError("删除剧情类型详情:"+sysTvData.getName()+" 失败,有存在的该剧情类型的内容");
                    return error("删除剧情类型详情:"+sysTvData.getName()+" 失败,有存在的该剧情类型的内容");
                }else{
                    sysOperLogService.insertOperlogSuccess("删除剧情类型详情:"+sysTvData.getName()+" 成功");
                    sysTvDataService.deleteSysTvDataById(dataId);
                }
            }else if(SysTvType.songDefaultId.equals(String.valueOf(typeId))){
                SysMusicData sysMusicData = new SysMusicData();
                sysMusicData.setTypeData(dataId);
                List<SysMusicData> musicList = sysMusicDataService.selectSysMusicDataList(sysMusicData);
                if (musicList != null && musicList.size() > 0) {
                    sysOperLogService.insertOperlogError("删除内容类型详情:"+sysTvData.getName()+" 失败,有存在的该类型的内容");
                    return error("删除内容类型详情:"+sysTvData.getName()+" 失败,有存在的该类型的内容");
                }else{
                    sysOperLogService.insertOperlogSuccess("删除内容类型详情:"+sysTvData.getName()+" 成功");
                    sysTvDataService.deleteSysTvDataById(dataId);
                }
            }else{
                sysTvDataService.deleteSysTvDataById(dataId);
            }
        }
        return success("删除成功");
    }

    @PostMapping("/getContentTypeData")
    public AjaxResult getContentTypeData(@RequestBody SysTvData sysTvData){

        sysTvData.setStatus(UserStatus.OK.getCode());
        List<SysTvData> sysTvDataList = sysTvDataService.selectSysTvDataList(sysTvData);
        return success(sysTvDataList);
    }
    /**
     * 获取字典选择框列表
     */
    @GetMapping("/optionselect")
    public AjaxResult optionselect()
    {
        List<SysTvType> sysTvTypes = sysTvTypeService.selectSysTvTypeList(new SysTvType());
        return success(sysTvTypes);
    }
}
